package com.tao.util.pack;

import com.tao.domain.User;
import com.tao.util.ThreadLostener;
import com.tao.util.Utils;
import com.tao.util.pack.send.GroupMessage;
import com.tao.util.pack.send.impl.GroupMessageInterImpl;

public class HeartSendThread extends Thread {

	private StringSubClass ss = new StringSubClass();
	private boolean beat = true;
	private OnSendMsgListener mSendMsgListener;
	private OnExitSendMsgListener mExitSendMsgListener;
	private WeChatClass wechat;
	User user; 
	public interface OnSendMsgListener {// 接收新消息监听器
		void onNewMsg(String text);

		void startBeat();
	}

	public interface OnExitSendMsgListener {// 用户退出监听器
		void onExitMsg(String text);

	}

	public HeartSendThread(User user) {
		this.user = user;
	}

	public void setmSendMsgListener(OnSendMsgListener mSendMsgListener) {
		this.mSendMsgListener = mSendMsgListener;
	}

	public void setmExitSendMsgListener(OnExitSendMsgListener mExitSendMsgListener) {
		this.mExitSendMsgListener = mExitSendMsgListener;
	}

	@Override
	public void run() {
		if (mSendMsgListener != null) {
			mSendMsgListener.startBeat();
		}
		
		if(beat==false){
			String name = "close"+this.getName();
			if(ThreadLostener.heartSendThreadMap.containsKey(name)){				
				ThreadLostener.heartSendThreadMap.remove(name);
			}
			System.out.println("----------------移除线程----------"+name);
		}
		
		while (beat) {

			GroupMessage gMessage = new GroupMessageInterImpl();

			String result = null;
			Msg msg = new Msg();
			msg.setContent("" + System.currentTimeMillis());
			msg.setToUserName("filehelper");
				result = gMessage.sendGroupMsg(user, msg);
				mSendMsgListener.onNewMsg(user.getNickName()+":发信息:"+Utils.getSystemTime());
				System.out.println("result"+result);
			String retcode = "";
			try {
				retcode = ss.subStringOne(result,"Ret\":","\"ErrMsg\":").trim();
			} catch (Exception e) {
				//continue;
				System.out.println("获取retcode失败");
			}
			System.out.println(retcode+retcode.equals("1100") );
			// 线程关闭
			if (retcode.equals("1100,") || retcode.equals("1101,")) {
				beat = false;
				mExitSendMsgListener.onExitMsg(user.getNickName()+":"+Utils.getSystemTime());
			}

			System.out.println(user.getNickName()+":"+"发信息:"+Utils.getSystemTime());
			try {
				// 线程休眠15分钟再请求网络发送1000*60*15 = 900000
				sleep(900000);

			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}// while
		
		System.out.println(this.getName()+"关闭!!!!!!!!!!!!!!!!!!!");
	}

	public boolean isBeat() {
		return beat;
	}

	public void setBeat(boolean beat) {
		this.beat = beat;
	}

	public StringSubClass getSs() {
		return ss;
	}

	public void setSs(StringSubClass ss) {
		this.ss = ss;
	}

	public WeChatClass getWechat() {
		return wechat;
	}

	public void setWechat(WeChatClass wechat) {
		this.wechat = wechat;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public OnSendMsgListener getmSendMsgListener() {
		return mSendMsgListener;
	}

	public OnExitSendMsgListener getmExitSendMsgListener() {
		return mExitSendMsgListener;
	}
}
